﻿using GraduationProject.Model;
using GraduationProject.Webs.Areas.Admin;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Controllers
{
    public class NovelController : BaseController
    {
        #region 字段
        public readonly GraduationProject.RDBMS.Novel _novelService = new RDBMS.Novel();
        #endregion

        // GET: Novel
        public ActionResult Index()
        {
            return View();
        }
        // GET:SearchType
        public ActionResult TypeIndex(int type)
        {
            SortList[] Sort;
            //获取分类标记数据
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {

                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            ViewData["UserModel"] = CurrentUser;
            //获取置顶数据
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount))
                {
                    var NovelModel = Novels.ToList();
                    var Model = from item in NovelModel
                                where item.IsTop = true
                                orderby item.ClickNum descending
                                select item;
                    ViewData["TopModel"] = Model;
                }
            }
            ViewData["Usertype"] = type;
            return View();
        }
        /// <summary>
        /// 分页获取文章数据(根据点赞数--1,反对数--2,点击量--3,用户发表时间--4)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByaTypePage(int curr, int limit, string type)
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount, curr, limit, 0, (RDBMS.NovelSortType)Enum.Parse(typeof(RDBMS.NovelSortType), type)))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Novels });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }

        /// <summary>
        /// 分页获取文章数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByPage(int page, int limit)
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount, page, limit))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Novels });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 文章数据(SortID景区编号类型)页面 点击景区导航栏调整
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectNovelByType(int type)
        {
            SortList[] Sort;
            //获取分类标记数据
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {

                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            ViewData["UserModel"] = CurrentUser;
            //获取置顶数据
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount))
                {
                    var NovelModel = Novels.ToList();
                    var Model = from item in NovelModel
                                where item.IsTop = true
                                orderby item.ClickNum descending
                                select item;
                    ViewData["TopModel"] = Model;
                }
            }
            ViewData["Usertype"] = type;
            return View();
        }
        /// <summary>
        /// 根据用户ID访问文章列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectNovelByUserId(int UserID)
        {
            SortList[] Sort;
            //获取分类标记数据
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {

                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            ViewData["UserModel"] = CurrentUser;
            ViewData["UserID"] = UserID;
            //获取置顶数据
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount))
                {
                    var NovelModel = Novels.ToList();
                    var Model = from item in NovelModel
                                where item.IsTop = true
                                orderby item.ClickNum descending
                                select item;
                    ViewData["TopModel"] = Model;
                }
            }
            return View();
        }
        /// <summary>
        /// 分页获取文章数据(SortID景区编号类型)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByTypePage(int curr, int limit, int type)
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount, curr, limit,NovelSortTypeID: type))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Novels });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 分页获取文章数据(用户编号)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByUserIDPage(int curr, int limit, int UserID)
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount, curr, limit, CreateUserID:UserID))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Novels });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 获取文章数据(热门)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByHot()
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount,1,20,0,RDBMS.NovelSortType.ClickNum,0,0))
                {
                    var NovelModel = Novels.ToList();
                    var Model = from item in NovelModel
                                orderby item.ClickNum descending
                                select item;
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Model });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 获取文章数据(置顶)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByTop()
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount))
                {
                    var NovelModel = Novels.ToList();
                    var Model = from item in NovelModel
                                orderby item.IsTop == true descending
                                select item;
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Model });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 发布帖子页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult InsertNovel()
        {
            //获取分类标记数据
            SortList[] Sort;
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {

                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            ViewData["UserModel"] = CurrentUser;
            return View();
        }
        /// <summary>
        /// 帖子插入(ValidateInput用来关闭富文本编辑器的html验证 )
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(Novel model)
        {
            model.CreateTime = DateTime.Now;
            if (CurrentUser != null)
            {
                model.CreateUser = CurrentUser.UserID;
            }
            model.IsBan = true;
            var models = new Novel[] { model };
            bool result;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryInsertNovel(out result, models))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "发布成功" });
                    }
                }
            }
            return Json(new { code = 1, msg = "发布失败，请稍后重试！" });
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadImage(HttpPostedFileBase file)
        {
            string path;
            if (file != null)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/UploadImg"), Path.GetFileName(file.FileName));
                file.SaveAs(filePath);
                path = "../UploadImg/" + Path.GetFileName(file.FileName);
                return Json(new { code = 0, msg = "成功", data = new { src = path } });
            }
            else
            {
                path = "../UploadImg";
                return Json(new { code = 1, msg = "抱歉,上传失败", data = new { src = path } });
            }
        }

        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <param name="NovelID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateDisAgreeNum(int NovelID)
        {
            if (CurrentUser == null)
            {
                return Json(new { code = 1, msg = "你未登陆，无法进行点赞！" });
            }
            bool result;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryAddAgreeNum(out result, NovelID, CurrentUser.UserID))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "点赞成功" });
                    }
                    else
                    {
                        return Json(new { code = 0, msg = "已经点赞，不可重复点击" });
                    }
                }
            }
            return Json(new { code = 1, msg = "点赞失败，请稍后重试！" });
        }
        /// <summary>
        /// 文章点反对
        /// </summary>
        /// <param name="NovelID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SubDisAgreeNum(int NovelID)
        {
            if (CurrentUser == null)
            {
                return Json(new { code = 1, msg = "你未登陆，无法进行反对！" });
            }
            bool result;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySubDisAgreeNum(out result, NovelID, CurrentUser.UserID))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "反对成功" });
                    }
                    else
                    {
                        return Json(new { code = 0, msg = "已经点反对，不可重复点击" });
                    }
                }
            }
            return Json(new { code = 1, msg = "反对失败，请稍后重试！" });
        }
    }
}